home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / alv.sun / alv.lha / src / rasremap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-08  |  3.5 KB  |  126 lines

  1. /**************************************************************************/
  2. /*  Based on the program blend.c by Phill Everson, from the ALV version   */
  3. /*  2.1 suite of public-domain image processing programs.                 */
  4. /*  Program to read a rasterfile and (optionally) a lookup table (LUT)    */
  5. /*  file produced by the program cst.  Using the values from either the   */
  6. /*  rasterfile's LUTs (=default) or the input LUTs, map pixel values to   */
  7. /*  their LUT equivalents, then substitute a 1:1 mapping LUT and write    */
  8. /*  out the file.  This allows interactively thresholded and contrast     */
  9. /*  stretched images to be used in programs which ignore LUTs.            */
  10. /*                                                                        */
  11. /*                              ras_remap.c                               */
  12. /*                                                                        */
  13. /*  Bryan Dawson, Keele University Geography Dept.                        */
  14. /*                                                        30/3/89         */
  15. /**************************************************************************/
  16.  
  17. #include <stdio.h>
  18. #include "defs.h"
  19.  
  20. har * progname;
  21. har * malloc();
  22. nt header[8];
  23. nsigned char red[256],green[256],blue[256];
  24. ong filelen;
  25. nsigned char * image;
  26.  
  27. #ifdef STANDALONE
  28. ain(argc, argv, envp)
  29. #else
  30. asremap_main(argc, argv, envp)
  31. #endif
  32.     int   argc;
  33.     char  **argv;
  34.     char  **envp;
  35. {
  36.     register int x,y;
  37.     int   lutfile = 0;
  38.     FILE  *f1, *f2;
  39.  
  40.     f1 = stdin;
  41.     f2 = stdin;
  42.     progname = strsave(argv[0]);
  43.     parse_profile(&argc, argv, envp);
  44.  
  45.     while ((gc = getopt(argc, argv, "f:l:")) != EOF)
  46.         switch (gc) 
  47.         {
  48.         case 'f':
  49.             if ((f1 = fopen(optarg, "r")) == NULL)
  50.                 error("Can't open %s\n", optarg);
  51.             break;
  52.         case 'l':
  53.             if ((f2 = fopen(optarg, "r")) == NULL) lutfile = 0 ; 
  54.                 else lutfile = 1;
  55.             break;
  56.         case '?':
  57.             errflag++;
  58.             break;
  59.         } /* end switch */
  60.  
  61. f (errflag)
  62.     error((char *) 0, "Usage: %s: [-f imgfile] [-l LUTfile] [outfile] \n", progname);
  63.  
  64. /* read rasterfile header */
  65.  = fread(header,sizeof(int),8,f1);;
  66. f (header[0] != 0x59a66a95) error("This isn't a Sun rasterfile !");
  67. ilelen = (long)header[4];
  68. mage = (u_char *)malloc(filelen); /* allocate space for image data */
  69.  
  70. /* if there is a LUT on the file, and it's needed, read it, else skip it */
  71. f ((lutfile == 0) && (header[7] == 768)) 
  72.     {
  73.     x = fread(red,1,256,f1);
  74.     x = fread(green,1,256,f1);
  75.     x = fread(blue,1,256,f1);
  76.     } else x = fseek(f1,header[7],1);
  77.  
  78. /* read image data */
  79.  = fread(image,1,filelen,f1);    
  80.  
  81. /* if external LUTs are to be used, read them */
  82. f (lutfile == 1)
  83.     {
  84.     x = fread(red,1,256,f2);
  85.     x = fread(green,1,256,f2);
  86.     x = fread(blue,1,256,f2);
  87.     }
  88.  
  89. /* if there is still no LUT, create a dummy one */
  90. f ((lutfile == 0) && (header[6] == RMT_NONE))
  91.     for (x=0;x<256;x++) red[x]=green[x]=blue[x] = x;
  92.     
  93. emap();
  94. or (x=0;x<256;x++) red[x]=green[x]=blue[x] = x;
  95. riteit();
  96. ree(image);
  97. xit();
  98. }
  99.  
  100. /* routine to map pixel values to their LUT equivalents */
  101. emap()
  102. {
  103. egister int temp;
  104. egister long p;
  105.  
  106. or (p = 0L ; p < filelen ; p++)
  107.     {
  108.     temp = red[(int)*(image+p)];
  109.     *(image+p) = temp;
  110.     }
  111. }
  112.  
  113. /* routine to write out the three components of a rasterfile */
  114. riteit()
  115. {
  116. eader[6] = 1;/* RMT_EQUAL_RGB */
  117. eader[7] = 768;
  118. write(header,4,8,stdout);
  119. write(red,1,256,stdout);/* greyscale image */
  120. write(red,1,256,stdout);
  121. write(red,1,256,stdout);
  122. write(image,1,filelen,stdout);
  123. }
  124.  
  125.  
  126.